

package com.remoteblogger.wordpress;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Vector;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;

/*
 * WordPress client object.
 * @author  Prasath Nadarajah
 *
 */

public class WPClient {

    private XmlRpcClientConfigImpl config;
    private XmlRpcClient client;

    private String userName;
    private String passWord;
    private String blogURL;
   
    private Vector blogParam;

    private Object userDetails;

    public WPClient(String uname, String pword, String url) {

        try {
            //configuring xml-rpc client
            config = new XmlRpcClientConfigImpl();
            config.setServerURL(new URL(url.concat("/xmlrpc.php")));
            client = new XmlRpcClient();
            client.setConfig(config);

            //creating the first parameter
            Vector loginParam = new Vector();
            loginParam.add(uname);
            loginParam.add(pword);

            userDetails = client.execute("wp.getUsersBlogs", loginParam);
            NotifyDescriptor d = new NotifyDescriptor.Message("Successfully created new weblog");
            DialogDisplayer.getDefault().notify(d);
            
            // connection success we are building other parameters as well configuring parameters
            this.userName = uname;
            this.passWord = pword;
            this.blogURL = url;

            blogParam = new Vector();
            blogParam.add(blogID());
            blogParam.add(userName);
            blogParam.add(passWord);
            blogParam.add(new HashMap());
            blogParam.add(true);

            /**
            * @todo XMLRPC enabled in site check implement
            */

        } catch (XmlRpcException ex) {
            System.out.println("Error WPClient:"+ex);
            NotifyDescriptor d = new NotifyDescriptor.Message("Username Password not valid"+uname+"::"+pword+"::"+url);
            DialogDisplayer.getDefault().notify(d);
        } catch(MalformedURLException ex) {
            System.out.println("Exception WPClient:"+ex);
            NotifyDescriptor d = new NotifyDescriptor.Message("The site URL you entered is not valid");
            DialogDisplayer.getDefault().notify(d);
        }

        

    }

    public boolean isAdmin() {
        return ((Boolean)((HashMap)((Object[])userDetails)[0]).get("isAdmin")).booleanValue();
    }

    public String blogName() {
        return (String) ((HashMap)((Object[])userDetails)[0]).get("blogName");
    }

    private  int blogID() {
        return Integer.parseInt((String) ((HashMap)((Object[])userDetails)[0]).get("blogid"));
    }

    public void  createPost(HashMap params,boolean publish) {
        String postID;
        blogParam.set(3, params);
        blogParam.set(4,publish);
        try {
            // there is no use of the return value
            postID  = (String) client.execute("metaWeblog.newPost", blogParam);
            NotifyDescriptor d = new NotifyDescriptor.Message("Successfully Published PostID: "+postID);
            DialogDisplayer.getDefault().notify(d);
        } catch (XmlRpcException ex) {
            NotifyDescriptor d = new NotifyDescriptor.Message("Could not publish this post"+ex);
            DialogDisplayer.getDefault().notify(d);
        }

    }

}
